package com.xln.demo9多态的应用综合;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Game game = new Duizhan();
        Game game1 = new Sangsi();
//        向上转型;安全转换

//        game.play();
//        game.play();
//        System.out.printf("名字变量："+game.name);
        Duizhan duizhan = (Duizhan) game;
        duizhan.xiabao();
//        向下转型：非安全转换
//      解决子类对象访问独有方法和属性的问题
//      缺陷:如果转型后的类型和对象真实对象的类型不是同一种类型，那么在运行代码时，就会出现ClassCastException
//
//会报错 Duizhan duizhan1 = (Duizhan) game1;
//      duizhan1.xiabao();

//        Java建议强转转换前使用 instanceof 判断当前对象的真实类型,再进行强制转换
        if (game1 instanceof Duizhan) {
            Duizhan duizhan1 = (Duizhan) game1;
        } else {
            System.out.printf("不是同一类型，game1不是cf!\n");
        }

        Diaomaozhang diaomaozhang = new Diaomaozhang();

        diaomaozhang.play(game1);
        System.out.printf("\n");
        diaomaozhang.play(duizhan);
        System.out.printf("================\n");
        System.out.printf("你想要叼毛张玩什么游戏？\n1:CF  2:求生之路\n");
        Scanner scanner = new Scanner(System.in);
        String youxi = scanner.next();
        System.out.printf(youxi+"\n");
        diaomaozhang.play(diaomaozhang.selectgm(youxi));
//        diaomaozhang.play(game2);
        System.out.printf("================\n");
    }
}